﻿import requests
from rebot import rebotSendMessage


# today='2024-10-15'
# yesterday='2024-10-14'

today=input("请输入要获取的自动化数据时间(如2024-01-02):")
yesterday=input("请输入要比对的自动化数据时间(如2024-01-01):")
test=input("是不是在测试？(y/n)")
if test!='n':
    rebot_url="https://xz.wps.cn/api/v1/webhook/send?key=f4b5d2acf9f0150a3e507e8797369018"
else:
    rebot_url = "https://xz.wps.cn/api/v1/webhook/send?key=7b5145844387651189f3d387671b5a28"

# 机器人发消息 关闭时直接输出
rebotMode=1
# 工作链接
# rebot_url = "https://xz.wps.cn/api/v1/webhook/send?key=7b5145844387651189f3d387671b5a28"
# 测试链接
# rebot_url="https://xz.wps.cn/api/v1/webhook/send?key=f4b5d2acf9f0150a3e507e8797369018"
url=f"https://automation-api.testplus.cn/api/pipeline/304/performance/trend?startTime={yesterday}+00:00:00&endTime={today}+23:59:59&projectId=yycs"
headers = {
   'Host': 'automation-api.testplus.cn'
}
scenes=['惠安镇','辽东郡','秀水郡']
deviceNames=['园区-GPU-2070','园区-GPU-1060','园区-GPU-1050ti']
default_message=f"\n\n## 月影传说{today}场景性能情况汇总\n\n"
message=f"\n\n## 月影传说{today}场景性能情况汇总\n\n"
newPerfeyeUrlList=[]
lowPerfeyeUrlList=[]

def disposeFPS(indicator):
    indicatorStr=""
    if indicator>=3 and rebotMode:
        indicatorStr+="<font color='green'>**"
    if indicator<=-3 and rebotMode:
        indicatorStr+="<font color='red'>**"
    if indicator>0:
        indicatorStr+='提升'
    else:
        indicatorStr+='下降'
    indicatorStr+=f'{round(abs(indicator),2)}'
    if abs(indicator)>=3 and rebotMode:
        indicatorStr+="**</font>"
    return indicatorStr
def disposeMemory(indicator):
    indicatorStr=""
    if indicator<=-100 and rebotMode:
        indicatorStr+="<font color='green'>**"
    if indicator>=100 and rebotMode:
        indicatorStr+="<font color='red'>**"
    if indicator>0:
        indicatorStr+='增加'
    else:
        indicatorStr+='减少'
    indicatorStr+=f'{round(abs(indicator),2)}'+'MB'
    if abs(indicator)>=100 and rebotMode:
        indicatorStr+="**</font>"
    return indicatorStr
def disposeJank(indicator):
    indicatorStr=""
    if indicator<=-100 and rebotMode:
        indicatorStr+="<font color='green'>**"
    if indicator>=100 and rebotMode:
        indicatorStr+="<font color='red'>**"
    if indicator>0:
        indicatorStr+='增加'
    else:
        indicatorStr+='减少'
    indicatorStr+=f'{round(abs(indicator),2)}'
    if abs(indicator)>=100 and rebotMode:
        indicatorStr+="**</font>"
    return indicatorStr
def printIndicatorData(data):
    global message
    global newPerfeyeUrlList
    global lowPerfeyeUrlList
    if not rebotMode:
        print(f"月影传说{today}场景性能情况汇总")
    for scene in scenes:
        if rebotMode:
            message+=f'------------------------【 {scene} 】------------------------<br>'
        else:
            print(f'------------------------【 {scene} 】------------------------')
        for i in range(len(deviceNames)):
            device_name=deviceNames[i]
            if deviceNames[i] not in data[scene]:
                if rebotMode:
                    message+=f'{device_name} : ---------------------------------------------未出数据<br>'
                else:
                    print(f'{device_name} : ---------------------------------------------未出数据')
                continue
            yesterdayData=yesterday in data[scene][device_name]
            todayData=today in data[scene][device_name]
            if yesterdayData:
                data1=data[scene][device_name][yesterday]
            if todayData:
                data2=data[scene][device_name][today]
            if yesterdayData and todayData:
                tp90 = disposeFPS(data2['tp90'] - data1['tp90'])
                memory = disposeMemory(data2['memory'] - data1['memory'])
                jank = disposeJank(data2['jank'] - data1['jank'])
                lowPerfeyeUrlList.append(f'https://perfeye.console.testplus.cn/case/{data1['perfeye']}/report?appKey=yycs')
                newPerfeyeUrlList.append(f'https://perfeye.console.testplus.cn/case/{data2['perfeye']}/report?appKey=yycs')
                if rebotMode:
                    message+=f' [{device_name}](https://perfeye.console.testplus.cn/case/{data2['perfeye']}/report?appKey=yycs):'
                    message+=f' {data2['tp90']}({tp90}) ,  {data2['memory']}({memory}) ,  {data2['jank']}({jank})<br>'
                else:
                    print(f'{device_name} : {data2['tp90']}({tp90}) ,  {data2['memory']}({memory}) ,  {data2['jank']}({jank})')
                    # print(f'https://perfeye.console.testplus.cn/case/{data1['perfeye']}/report?appKey=yycs')
                    # print(f'https://perfeye.console.testplus.cn/case/{data2['perfeye']}/report?appKey=yycs')
            elif todayData:
                newPerfeyeUrlList.append(f'https://perfeye.console.testplus.cn/case/{data2['perfeye']}/report?appKey=yycs')
                if rebotMode:
                    message+=f' [{device_name}](https://perfeye.console.testplus.cn/case/{data2['perfeye']}/report?appKey=yycs):'
                    message+=f' {data2['tp90']},  {data2['memory']},  {data2['jank']}-----------<font color="grey">**【{yesterday}】无数据**</font><br>'
                else:
                    print(f'{device_name} : {data2['tp90']},  {data2['memory']},  {data2['jank']}\t\t【{yesterday}】无数据')
            elif yesterdayData:
                # lowPerfeyeUrlList.append(f'https://perfeye.console.testplus.cn/case/{data1['perfeye']}/report?appKey=yycs')
                if rebotMode:
                    message+=f' [{device_name}](https://perfeye.console.testplus.cn/case/{data1['perfeye']}/report?appKey=yycs):'
                    message+=f' {data1['tp90']},  {data1['memory']},  {data1['jank']}<font color="grey">**【{today}】未出数据**</font><br>'
                else:
                    print(f'{device_name} : {data1['tp90']},  {data1['memory']},  {data1['jank']}\t\t【{today}】未出数据')
    if not rebotMode:
        for i in range(len(newPerfeyeUrlList)):
            if len(lowPerfeyeUrlList)>i:
                print(f'{lowPerfeyeUrlList[i]}')
            print(f'{newPerfeyeUrlList[i]}')


def getIndicator_diction(data):
    indicator_data = {}  
    if 'data' not in data:
        print('请求失败')
        return False
    for item in data['data']:
        case_name = item['caseName']  
        if case_name not in scenes:
            continue
        device_name = item['deviceName']  
        create_time = item['createTime']  
        tp90 = item['LabelFPS.TP90']  
        jank = item['LabelFPS.Jank(/10min)']  
        peak_memory = item['LabelMemory.PeakMemory(MB)']  
        perfeye = item['perfeye']  
        if case_name not in indicator_data:  
            indicator_data[case_name] = {}  
        if device_name not in indicator_data[case_name]:  
            indicator_data[case_name][device_name] = {}  
        if create_time not in indicator_data[case_name][device_name]:  
            indicator_data[case_name][device_name][create_time] = {}  
        indicator_data[case_name][device_name][create_time] = {  
            'tp90': round(float(tp90),2),  
            'jank': round(float(jank),2),  
            'memory': round(float(peak_memory),2),  
            'perfeye': perfeye  
        }  
    return indicator_data


message=default_message
newPerfeyeUrlList=[]
lowPerfeyeUrlList=[]

response = requests.request("GET", url, headers=headers)
data = response.json() 
print(data)
data=getIndicator_diction(data)
if data:
    printIndicatorData(data)
    if rebotMode:
        rebotSendMessage(rebot_url,message)
